﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace EnableItemSchedulingOnSite
{
    class Program
    {

        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("usage: Program.exe <http://localhost>");
            }

            Console.WriteLine("http://edwin.vriethoff.net/2009/05/18/enable-item-scheduling-by-code/");
            using (SPSite site = new SPSite(args[0]))
            {
                Console.WriteLine("Site Url: " + site.ServerRelativeUrl);
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    foreach (SPWeb web in site.AllWebs)
                    {
                        if (PublishingWeb.IsPublishingWeb(web))
                        {
                            Console.WriteLine(web.ServerRelativeUrl);

                            PublishingWeb pweb = PublishingWeb.GetPublishingWeb(web);
                            SPList list = pweb.PagesList;

                            //Enable moderation and minor versions
                            list.EnableModeration = true;
                            list.EnableMinorVersions = true;
                            list.Update();

                            //get type information
                            Type scheduledItemType = typeof(Microsoft.SharePoint.Publishing.Internal.ScheduledItemEventReceiver);
                            string assemblyName = scheduledItemType.Assembly.FullName.ToString();
                            string className = scheduledItemType.FullName;

                            //Get the eventreceivers
                            SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;

                            //Register the updating event
                            SPEventReceiverDefinition updateDef = eventReceivers.Add();
                            updateDef.Name = "Item Updating Event Handler For Scheduling";
                            updateDef.Type = SPEventReceiverType.ItemUpdating;
                            updateDef.Assembly = assemblyName;
                            updateDef.Class = className;
                            updateDef.Update();

                            SPEventReceiverDefinition addDef = eventReceivers.Add();
                            addDef.Name = "Item Added Event Handler For Scheduling";
                            addDef.Type = SPEventReceiverType.ItemAdded;
                            addDef.Assembly = assemblyName;
                            addDef.Class = className;
                            addDef.Update();

                            //Get the field guids of the startdate and expirydate
                            Guid startDateGuid = Microsoft.SharePoint.Publishing.FieldId.StartDate;
                            Guid expiryDateGuid = Microsoft.SharePoint.Publishing.FieldId.ExpiryDate;

                            //update the visibility of the startdate
                            SPField startDateField = list.Fields[startDateGuid];
                            startDateField.Hidden = false;
                            startDateField.Update();

                            //update the visibility of the expirydate
                            SPField expiryDateField = list.Fields[expiryDateGuid];
                            expiryDateField.Hidden = false;
                            expiryDateField.Update();

                        }
                    }
                });
            }
            Console.WriteLine("Finished");

        }
    }
}
